﻿Imports AutoCAD

Public Class form_孔位工具

    'Service
    Private mySqlService As IMySqlService = New MySqlServiceImpl
    Private cadApplicationService As ICadApplicationService = New CadApplicationServiceImpl
    Private cadDrawHoleService As ICadDrawHoleService = New CadDrawHoleServiceImpl
    Private cadEntityDataService As ICadEntityDataService = New CadEntityDataServiceImpl
    Private entityConverterService As IEntityConverterService = New EntityConverterServiceImpl


    'Mapper
    Private holeSchemeHeadMapper As HoleSchemeHeadMapper = New HoleSchemeHeadMapper
    Private holeSchemeLineMapper As HoleSchemeLineMapper = New HoleSchemeLineMapper

    'DataSource
    Private dt_HoleSchemeHead As DataTable
    Private dt_HoleSchemeLine As DataTable

    Private Sub form_孔位工具_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        lbl_SchemeName.Text = ""
    End Sub


    ''' <summary>
    ''' 设置数据源
    ''' </summary>
    Sub SetDataSource_LBHoleSchemeHead()
        dt_HoleSchemeHead = holeSchemeHeadMapper.Select
        With lb_HoleSchemeHead
            .DataSource = dt_HoleSchemeHead
            .DisplayMember = "scheme_name"
            .ValueMember = "head_id"
        End With
    End Sub

    Private Sub btn_refreshData_Click(sender As Object, e As EventArgs) Handles btn_refreshData.Click
        SetDataSource_LBHoleSchemeHead()
    End Sub

    Private Sub tb_HoleSchemeHeadFilterStr_TextChanged(sender As Object, e As EventArgs) Handles tb_HoleSchemeHeadFilterStr.TextChanged
        If dt_HoleSchemeHead Is Nothing Then Exit Sub
        dt_HoleSchemeHead.DefaultView.RowFilter = $"scheme_name like '%{tb_HoleSchemeHeadFilterStr.Text}%'"
    End Sub



    Private Sub btn_draw_Click(sender As Object, e As EventArgs) Handles btn_draw.Click

        Dim entitys = cadApplicationService.GetCurrentSelectSet() '图元列表
        Dim holes As DataTable = dt_HoleSchemeLine '孔数据

        Dim plate As AcadBlockReference
        Dim holeEntity As HoleEntity
        Dim sx, sy, sz As Double
        For Each ent As AcadEntity In entitys
            If cadEntityDataService.IsPlate(ent) Then
                plate = ent
                sx = plate.XScaleFactor
                sy = plate.YScaleFactor
                sz = plate.ZScaleFactor

                For Each dr_hole As DataRow In holes.Rows
                    dr_hole("sx") = sx
                    dr_hole("sy") = sy
                    dr_hole("sz") = sz
                    holeEntity = entityConverterService.ConverterHoleEntity(dr_hole)
                    cadDrawHoleService.DrawHole(holeEntity.face, plate, holeEntity.px, holeEntity.py, holeEntity.pz, holeEntity.diameter, holeEntity.depth)

                Next dr_hole
            End If
        Next ent

    End Sub

    Private Sub lb_HoleSchemeHead_MouseClick(sender As Object, e As MouseEventArgs) Handles lb_HoleSchemeHead.MouseClick
        If lb_HoleSchemeHead.Items.Count < 1 Then Exit Sub
        lbl_SchemeName.Text = lb_HoleSchemeHead.SelectedItem("scheme_name")
        Dim headid As Integer = lb_HoleSchemeHead.SelectedItem("head_id")
        dt_HoleSchemeLine = holeSchemeLineMapper.Select_ByHeadId(headid)
        dgv_HoleSchemeLine.DataSource = dt_HoleSchemeLine
        dgv_HoleSchemeLine.AutoResizeColumns()
    End Sub
End Class